using System.Text;
using System.Xml;

namespace Vibstudio.X4NET.Xml.Generation
{
    #region Namespaces

    

    #endregion

    internal class InstanceElement : InstanceGroup
    {
        #region Fields

        private StringBuilder _comment;
        private bool _genNil = true;
        private XmlQualifiedName _xsiType = XmlQualifiedName.Empty;

        #endregion

        #region Ctor(s)

        internal InstanceElement()
        {
        }

        internal InstanceElement(XmlQualifiedName name)
        {
            QualifiedName = new XmlQualifiedName(name.Name, name.Namespace);
            ValueGenerator = null;
        }

        #endregion

        #region Properties

        internal StringBuilder Comment
        {
            get { return _comment ?? (_comment = new StringBuilder()); }
            set { _comment = value; }
        }

        internal InstanceAttribute FirstAttribute { get; private set; }

        internal bool GenNil
        {
            get { return _genNil; }

            set { _genNil = value; }
        }

        internal bool IsMixed { get; set; }

        internal bool IsNillable { get; set; }

        internal XmlQualifiedName XsiType
        {
            get { return _xsiType; }
            set { _xsiType = value; }
        }

        #endregion

        #region Methods

        internal void AddAttribute(InstanceAttribute attr)
        {
            if (FirstAttribute == null)
            {
                FirstAttribute = attr;
            }
            else
            {
                InstanceAttribute next = FirstAttribute;
                InstanceAttribute prev = null;
                while (next != null)
                {
                    prev = next;
                    next = next.NextAttribute;
                }
                if (prev != null)
                {
                    prev.NextAttribute = attr;
                }
            }
        }

        internal InstanceElement Clone(decimal occurs)
        {
            var newElem = (InstanceElement)MemberwiseClone();
            newElem.Occurs = occurs;
            newElem.Child = null;
            newElem.Parent = null;
            newElem.Sibling = null;
            return newElem;
        }

        #endregion
    }
}